﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CYTS.Aoyou.Framework.DAInterface;
using CYTS.Aoyou.Framework.Database;

namespace CYTS.Aoyou.Framework.DataAccess
{
    public class PerformanceDataAdapterForMongoDB : DataAdapterBase, IPerformanceDataAdapter, Framework.Database.IDataAdapter
    {

        public PerformanceDataAdapterForMongoDB(DBProvider provider)
            : base(provider)
        {
        }

        public void Delete<T>(string databaseName, string collectionName, string id)
        {
            string connectionString = string.Empty;
            if (!this.DicDBConnection.TryGetValue("MongoDBServer", out connectionString))
            {
                throw new Exception("未找到对应的数据库连接串");
            }
            MongoDBHelper.Delete(connectionString, databaseName, collectionName, id.ToString());
        }

        public T Get<T>(string databaseName, string collectionName, string id)
        {
            string connectionString = string.Empty;
            if (!this.DicDBConnection.TryGetValue("MongoDBServer", out connectionString))
            {
                throw new Exception("未找到对应的数据库连接串");
            }
            var result = MongoDBHelper.GetOne<T>(connectionString, databaseName, collectionName, id);

            return result;
        }

        public List<T> GetAll<T>(string databaseName, string collectionName)
        {
            string connectionString = string.Empty;
            if (!this.DicDBConnection.TryGetValue("MongoDBServer", out connectionString))
            {
                throw new Exception("未找到对应的数据库连接串");
            }
            var result = MongoDBHelper.GetAll<T>(connectionString, databaseName, collectionName);
            
            return result;
        }

        public void Set<T>(string databaseName, string collectionName, T value)
        {
            string connectionString = string.Empty;
            if (!this.DicDBConnection.TryGetValue("MongoDBServer", out connectionString))
            {
                throw new Exception("未找到对应的数据库连接串");
            }
            MongoDBHelper.InsertOne<T>(connectionString, databaseName, collectionName, value);
        }

    }
}
